package per.kongtouxu.service.Impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.kongtouxu.dao.FileTypeDao;
import per.kongtouxu.domain.pojo.AdminLog;
import per.kongtouxu.domain.pojo.FileType;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.service.FileTypeService;

import java.util.List;

@Service
public class FileTypeServiceImpl implements FileTypeService {
    @Autowired
    private AdminLogServiceImpl adminLogService;
    @Autowired
    private FileTypeDao fileTypeDao;
    @Override
    public PageBean<FileType> pageFind(String tailName, Integer startScore, Integer endScore, Integer currentPage, Integer pageSize) {

        //1.分页类
        PageBean<FileType> pageBean = new PageBean<>();
        //2.设置当前页码
        pageBean.setCurrentPage(currentPage);
        //3.设置每页显示条数
        pageBean.setPageSize(pageSize);
        //4.设置总记录条数
        int totalCount = fileTypeDao.findCountFileTypeByPage(tailName, startScore,endScore);
        pageBean.setTotalCount(totalCount);
        //5.设置PageBean中的实体数据
        //开始的记录条数=（当前页码-1）*每页显示条数
        int start = (currentPage - 1) * pageSize;
        List<FileType> list = fileTypeDao.findFileTypeByPage(tailName, startScore,endScore, new RowBounds(start, pageSize));
        pageBean.setList(list);
        //6.设置总页数   总页数= 总记录条数/每页显示条数=0 ？ 总记录条数/每页显示条数=0 ：（总记录条数/每页显示条数=0）+1
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        //7.返回
        return pageBean;
    }

    @Override
    public Integer addFileType(FileType fileType, AdminLog adminLog) {
        //1.获取Dao

        //2.执行
        Integer result = fileTypeDao.addFileType(fileType);
        if (result>0){
            adminLogService.saveAdminLog(adminLog);
        }
        return result;
    }

    @Override
    public Integer deleteFileType(Integer ftId, AdminLog adminLog) {
        //1.获取Dao
        //2.执行
        Integer result = fileTypeDao.deleteFileType(ftId);
        if (result>0){
            adminLogService.saveAdminLog(adminLog);
        }

        return result;
    }

    @Override
    public Integer updateFileType(FileType fileType, AdminLog adminLog) {
        //1.获取Dao
        //2.执行
        Integer result = fileTypeDao.updateFileType(fileType);
        if (result>0){
            adminLogService.saveAdminLog(adminLog);
        }
        return result;
    }
}
